import { _decorator, Component, Node, Enum, EventHandler, Sprite, Label } from 'cc';
import { DEV, EDITOR } from 'cc/env';
import { KeyAction, KeyActionInfo, KeyActionType } from './KeyActionDefine';
import { KeyCodeHandler } from '../base/KeyCodeHandler';
import { CtrlModeListener } from '../../core/GameInputListeners';
import GameInputMgr from '../../core/GameInputMgr';
import { InputType } from '../../core/InputConfig';

const { ccclass, property, executeInEditMode } = _decorator;

/**
 * 按键行为组件
 */
@ccclass('KeyActionHandler')
@executeInEditMode()
export class KeyActionHandler extends Component implements CtrlModeListener {
    @property
    isContinuous: boolean = false;

    @property
    protected _actionType: KeyActionType = KeyActionType.none;


    @property({ type: Enum(KeyActionType) })
    set actionType(value: KeyActionType) {
        this._actionType = value;
        this.reset();
        // LogUtils.log('KeyActionHandler set actionType ')
    }


    get actionType() {
        // LogUtils.log('KeyActionHandler get actionType ')
        return this._actionType;
    }

    @property({ tooltip: DEV && "行为关联的图片,没有可以为null" })
    @property(Sprite)
    actionTip: Sprite = null;

    @property({ tooltip: DEV && "行为关联的文本,没有可以为null" })
    @property(Label)
    actionName: Label = null;

    @property({ tooltip: DEV && "持续调用的间隔时间" })
    @property
    continuousDuration: number = 0;

    @property({ tooltip: DEV && "是否归keyboard管理" })
    @property
    isBelongKeyBoard: boolean = false;

    @property({ tooltip: DEV && "键盘是否显示按键提示" })
    @property
    isKeyboardShowTip: boolean = false;
    protected removeAllKeyCodeHandler() {
        if (EDITOR) {
            let handlers = this.node.getComponents(KeyCodeHandler)
            while (handlers.length > 0) {
                let element = handlers.shift();
                element.destroy();
            }
        }
    }

    reset() {
        // this.kcHandlers.length = 0;
        // this.initHandlers();
    }
    kcHandlers: KeyCodeHandler[] = [];

    private _model: KeyAction = null;//可以为空
    private _showTip: boolean = true;//是否在KeyboardInfo上显示按钮行为提示
    private _group: number = 0;//按键策略组

    public get model() {
        if (!this._model) {
            if (this.actionType == KeyActionType.none) {
                return null;
            }
            let name = KeyActionType[this.actionType]
            let keyAction: KeyAction = KeyActionInfo[name];
            if (keyAction) {
                this._model = keyAction;
            }
        }
        return this._model;
    }

    public set model(model) {
        this._model = model;
    }

    onChangeMode(mode: string): void {
        this.setKeyTipSprite();
        this.setBtnName();
    }

    protected isTipVisible() {
        return GameInputMgr.instance().type == InputType.STEAM || this.isKeyboardShowTip //|| DebugUtils.showKeyTip;
    }

    start() {
        if (!EDITOR) {
            this.onChangeMode(GameInputMgr.instance().type)
            GameInputMgr.instance().addCtrlModeListener(this)
        }
        this._group = GameInputMgr.instance().converter.curGroup;
    }

    onDestroy() {
        GameInputMgr.instance().removeCtrlModeListener(this)
    }

    protected setKeyTipSprite() {
        if (this.actionTip) {
            this.actionTip.node.active = this.isTipVisible()
            if (this.actionTip.node.active && this.actionType != KeyActionType.none && this.model) {
                // this.actionTip.node.active = true;

                // let keyName = GameInputMgr.getInstance().getKeyCodeImgByKeyValue(this.keyCode);
                // this.actionTip.spriteFrame = UIManager.getInstance().getKeyTip(this.getKeyName());
            }
        }

    }

    getKeyName() {
        let keyName = this.model[GameInputMgr.instance().getCfgName()]
        if (!keyName) {
            keyName = GameInputMgr.instance().getKeyCodeImg(this.model.keyCodes[0], this._group);
        }
        return keyName
    }
    protected setBtnName() {
        if (this.model && this.actionName && this.actionType != KeyActionType.none) {
            this.actionName.string = this.model.name;
        }
    }


    initHandlers() {

    }

    // public set isActive(value: boolean) {
    //     for (let i = 0; i < this.kcHandlers.length; i++) {
    //         this.kcHandlers[i].isActive = value;
    //     }
    // }

    public set showTip(value: boolean) {
        this._showTip = value;
    }

    public get showTip() {
        return this._showTip;
    }


}

